package com.gpt.article.service.impl;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gpt.article.domain.GptTopicTitle;
import com.gpt.article.domain.Lawyer;
import com.gpt.article.domain.SpiderEn;
import com.gpt.article.mapper.LawyerMapper;
import com.gpt.article.mapper.SpiderEnMapper;
import com.gpt.article.service.IGptTopicContentService;
import com.gpt.article.service.IGptTopicTitleService;
import com.gpt.article.service.ISpiderEnService;
import com.gpt.article.service.impl.taskHandle.task.GenTopicTasker;
import com.gpt.article.service.impl.taskHandle.testTask.SellTicket;
import com.ruoyi.common.core.domain.TreeSelect;
import com.ruoyi.common.utils.SecurityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Service;
import com.gpt.article.mapper.LawListMapper;
import com.gpt.article.domain.LawList;
import com.gpt.article.service.ILawListService;

/**
 * 全国律师列Service业务层处理
 * 
 * @author zhuzi
 * @date 2024-01-08
 */
@Service
public class LawListServiceImpl implements ILawListService 
{
    @Autowired
    private LawListMapper lawListMapper;

    @Autowired
    private SpiderEnMapper spiderEnMapper;

    private static ISpiderEnService spiderEnService;
    @Autowired
    public void setIGptTopicTitleService(ISpiderEnService spiderEnService) {
        LawListServiceImpl.spiderEnService = spiderEnService;
    }

    /**
     * 查询全国律师列
     * 
     * @param id 全国律师列主键
     * @return 全国律师列
     */
    @Override
    public LawList selectLawListById(Long id)
    {
        return lawListMapper.selectLawListById(id);
    }

    /**
     * 查询全国律师列列表
     * 
     * @param lawList 全国律师列
     * @return 全国律师列
     */
    @Override
    public List<LawList> selectLawListList(LawList lawList)
    {
        return lawListMapper.selectLawListList(lawList);
    }

    /**
     * 新增全国律师列
     * 
     * @param lawList 全国律师列
     * @return 结果
     */
    @Override
    public int insertLawList(LawList lawList)
    {
        return lawListMapper.insertLawList(lawList);
    }

    /**
     * 修改全国律师列
     * 
     * @param lawList 全国律师列
     * @return 结果
     */
    @Override
    public int updateLawList(LawList lawList)
    {
        return lawListMapper.updateLawList(lawList);
    }

    /**
     * 批量删除全国律师列
     * 
     * @param ids 需要删除的全国律师列主键
     * @return 结果
     */
    @Override
    public int deleteLawListByIds(Long[] ids)
    {
        return lawListMapper.deleteLawListByIds(ids);
    }

    /**
     * 删除全国律师列信息
     * 
     * @param id 全国律师列主键
     * @return 结果
     */
    @Override
    public int deleteLawListById(Long id)
    {
        return lawListMapper.deleteLawListById(id);
    }

    class CityTree{
        private String Code;
        private String Name;
        private List<LevelInfo> level;
        public String getCode() {
            return Code;
        }

        public void setCode(String code) {
            Code = code;
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }

        public List<LevelInfo> getLevel() {
            return level;
        }

        public void setLevel(List<LevelInfo> level) {
            this.level = level;
        }
    }

    class LevelInfo{
        private String Code;
        private String Name;

        public String getCode() {
            return Code;
        }

        public void setCode(String code) {
            Code = code;
        }

        public String getName() {
            return Name;
        }

        public void setName(String name) {
            Name = name;
        }
    }

    class LawerInfo{
        private String picpath;
        private String xzqh;
        private String lawofficeid;
        private String http;
        private String name;
        private String pkid;
        private String lawoffice;
        private String pic;
        private String years;
        private String avatar;
        private String goodat;

        public String getPicpath() {
            return picpath;
        }

        public void setPicpath(String picpath) {
            this.picpath = picpath;
        }

        public String getXzqh() {
            return xzqh;
        }

        public void setXzqh(String xzqh) {
            this.xzqh = xzqh;
        }

        public String getLawofficeid() {
            return lawofficeid;
        }

        public void setLawofficeid(String lawofficeid) {
            this.lawofficeid = lawofficeid;
        }

        public String getHttp() {
            return http;
        }

        public void setHttp(String http) {
            this.http = http;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getPkid() {
            return pkid;
        }

        public void setPkid(String pkid) {
            this.pkid = pkid;
        }

        public String getLawoffice() {
            return lawoffice;
        }

        public void setLawoffice(String lawoffice) {
            this.lawoffice = lawoffice;
        }

        public String getPic() {
            return pic;
        }

        public void setPic(String pic) {
            this.pic = pic;
        }

        public String getYears() {
            return years;
        }

        public void setYears(String years) {
            this.years = years;
        }

        public String getAvatar() {
            return avatar;
        }

        public void setAvatar(String avatar) {
            this.avatar = avatar;
        }

        public String getGoodat() {
            return goodat;
        }

        public void setGoodat(String goodat) {
            this.goodat = goodat;
        }
    }





    Runnable runnable = new Runnable() {

        int ticket = 5553;//100张票
        public void run() {
            while (true) {
                if (ticket < 218101) {
                    //卖出一张票并且打印
                    System.out.println(Thread.currentThread().getName() + "__" + ticket++);
                    try {
                        System.out.println("正在采集页码："+ticket);
                        spiderEn(ticket);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }
                }else{
                    System.out.println("结束...");
                    try {
                        Thread.sleep(5555555);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    };

    public  void runspider() {
        Thread thread1 = new Thread(runnable);
        Thread thread2 = new Thread(runnable);
        Thread thread3 = new Thread(runnable);
        Thread thread4 = new Thread(runnable);
        Thread thread5 = new Thread(runnable);
        Thread thread6 = new Thread(runnable);

        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
        thread6.start();
    }


    public static void main(String[] args) {
        String tt = "Global Hydrogen Fuel Cell Systems For Commercial Vehicles Companies Covered";
        System.out.println(tt.contains("Companies2"));
    }

    /**
     * 采集某个英文网站
     * @throws InterruptedException
     */
    public  void spiderEn(int page) throws InterruptedException {
//        int page = 1;
        Element el = null;

        Map<String, Object> row1 = null;
//        ArrayList<Map<String, Object>> rows = new ArrayList<Map<String, Object>>();

            System.out.println("正在访问页面："+page);
            String url= "https://www.qyresearch.com/api/product/list?keyWord=&orderBy=time&page="+page+"&pageSize=10";
            System.out.println("请求："+url);
            String json = getPostStr(url);
            JSONObject obj = JSONObject.parseObject(json);
            JSONObject obj2 = JSONObject.parseObject(String.valueOf(obj.get("data")));
            JSONArray jsona = JSONArray.parseArray(String.valueOf(obj2.get("product")));
        SpiderEn spiderEn = null;
            if(jsona.size()==0){
                System.out.println("结束.......");
                return;
            }
            loop2: for(Object li:jsona){
                JSONObject liobj = JSONObject.parseObject(String.valueOf(li));
                String goid = String.valueOf(liobj.get("goods_id"));
                String price = String.valueOf(liobj.get("price"));
                String published_date_format = String.valueOf(liobj.get("published_date_format"));
                if(published_date_format.indexOf("2024")==-1){
                    System.out.println("出现不是2024年...");
                    continue loop2;
                }
                if("3950.00".equals(price)&&published_date_format.indexOf("2024")!=-1){
//                if(published_date_format.indexOf("2024")!=-1){
                    String goosUrl = "https://www.qyresearch.com/reports/"+goid+"/green-preservatives";
                    String jsondetail = getPostStr(goosUrl);
                    System.out.println("发现匹配页面："+goosUrl);
                    Document doc = Jsoup.parse(jsondetail);
                     el = doc.select(".tabDiv").get(0);
                    try {
                        appenTxt(String.valueOf(el)+"<!--spider-->");
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
//                    System.out.println(el);
                     List<Element> liel = el.select("li");
                    if(liel.size()<15){
                        System.out.println("不符合条件，列数不匹配");
                        return;
                    }
                    String Title =null;
                    String Market_Size_in_2030 =null;
                    String CAGR =null;
                    String Companies  =null;
                    String Type =null;
                    String Application =null;

                    String preTitle = liel.get(2).text();
                    if(preTitle.contains("Report")){
                        Title = liel.get(3).text().trim();
                    }

                    String pre_Market_Size_in_2030 = liel.get(4).text().trim();
                    if(pre_Market_Size_in_2030.contains("Forecasted")){
                        Market_Size_in_2030 = getMiddleString(liel.get(5).text().trim(),"US$","million");
                    }

                    String pre_CAGR = liel.get(6).text();
                    if(pre_CAGR.contains("CAGR")){
                        CAGR = liel.get(7).text().trim();
                    }

//                    if(liel.size()>10){
                        String pre_cs = liel.get(10).text();
                        if(pre_cs.contains("Companies")){
                            String cs = liel.get(11).text().trim();
                            Companies = cs.replaceAll(", ","\n");
                        }
//                    }


//                    if(liel.size()>12){
                        String pre_type = liel.get(12).text();
                        if(pre_type.contains("Type")){
                            //根据p标签换行
                            String typeStr = "";
                            List<Element> elP = liel.get(13).select("p");
                            for(Element elp1:elP){
                                typeStr+=elp1.text()+"\n";
                            }
                            Type = typeStr;
                        }
//                    }




                    String pre_Application = liel.get(14).text();
                    if(pre_Application.contains("Application")){
                        String appstr = "";
                        List<Element> appstl = liel.get(15).select("p");
                        for(Element elp1:appstl){
                            appstr+=elp1.text()+"\n";
                        }

                        Application = appstr;
                    }

                    System.out.println("title:"+Title);
//                    System.out.println("Market_Size_in_2030:"+Market_Size_in_2030);
//                    System.out.println("CAGR:"+CAGR);
//                    System.out.println("Companies:"+Companies);
//                    System.out.println("Type:"+Type);
//                    System.out.println("Application:"+Application);

                    row1 = new LinkedHashMap<>();
                    row1.put("Title",Title);
                    row1.put("Market_Size_in_2030",Market_Size_in_2030);
                    row1.put("CAGR",CAGR);
                    row1.put("Companies",Companies);
                    row1.put("Type",Type);
                    row1.put("Application",Application);
                    spiderEn = new SpiderEn();
                    spiderEn.setTitle(Title);
                    spiderEn.setMarketSizeIn2030(Market_Size_in_2030);
                    spiderEn.setCAGR(CAGR);
                    spiderEn.setCompanies(Companies);
                    spiderEn.setType(Type);
                    spiderEn.setApplication(Application);
                    //存在数据库
                    spiderEnService.insertSpiderEn(spiderEn);

//                    rows.add(row1);
//                    Thread.sleep(5000);
                }

            }

//        writeExcel(rows);
    }


    public Boolean appenTxt(String txt) throws IOException {
        File f = new File("D:\\en.txt");
        if (f.exists()) {
//            System.out.print("文件存在");
        } else {
//            System.out.print("文件不存在");
            f.createNewFile();// 不存在则创建
        }
        BufferedWriter output = new BufferedWriter(new FileWriter(f,true));//true,则追加写入text文本

        output.write(txt);
        output.write("\r\n");//换行
        output.flush();
        output.close();
        return true;
    }

    public  Boolean writeExcel(ArrayList<Map<String, Object>> rows){
      try {
            ExcelWriter writer = ExcelUtil.getWriter("d:/spider_20240115.xlsx");
            writer.write(rows, true);
            writer.close();
        }catch (Exception e){
          System.out.println("error:"+e.getMessage());
        }
      return true;
    }

    public  String getMiddleString(String original, String first, String second) {
        int startIndex = original.indexOf(first) + first.length();
        int endIndex = original.indexOf(second);
        if (startIndex == -1 || endIndex == -1) {
            return null;
        }
        return original.substring(startIndex, endIndex);
    }

    public  void compare(String[] t1, String[] t2) {
        Set<String> set1 = new HashSet<>(Arrays.asList(t1));
        Set<String> set2 = new HashSet<>(Arrays.asList(t2));
        Set<String> set3 = new HashSet<>(set2);
        // set集合有去重特性
        set3.addAll(set1);
        //retainAll():保留包含在指定 collection 中的元素；
        set1.retainAll(set2);
        //	removeAll(); 移除 set 中那些包含在指定 collection 中的元素 ;
        set3.removeAll(set1);
        System.out.println(set3);
    }


    @Override
    public Boolean spiderENL(){
        new LawListServiceImpl().runspider();
          return true;
    }

    //采集律师
    @Override
    public Boolean spiderLaw() {
//        String cj = "[ {\"Code\": \"44\", \"Name\": \"广东省\", \"level\": [ {\"Code\": \"44\", \"Name\": \"全部\" }, {\"Code\": \"4401\", \"Name\": \"广州市\" }, {\"Code\": \"4402\", \"Name\": \"韶关市\" }, {\"Code\": \"4403\", \"Name\": \"深圳市\" }, {\"Code\": \"4404\", \"Name\": \"珠海市\" }, {\"Code\": \"4405\", \"Name\": \"汕头市\" }, {\"Code\": \"4406\", \"Name\": \"佛山市\" }, {\"Code\": \"4407\", \"Name\": \"江门市\" }, {\"Code\": \"4408\", \"Name\": \"湛江市\" }, {\"Code\": \"4409\", \"Name\": \"茂名市\" }, {\"Code\": \"4412\", \"Name\": \"肇庆市\" }, {\"Code\": \"4413\", \"Name\": \"惠州市\" }, {\"Code\": \"4414\", \"Name\": \"梅州市\" }, {\"Code\": \"4415\", \"Name\": \"汕尾市\" }, {\"Code\": \"4416\", \"Name\": \"河源市\" }, {\"Code\": \"4417\", \"Name\": \"阳江市\" }, {\"Code\": \"4418\", \"Name\": \"清远市\" }, {\"Code\": \"4419\", \"Name\": \"东莞市\" }, {\"Code\": \"4420\", \"Name\": \"中山市\" }, {\"Code\": \"4451\", \"Name\": \"潮州市\" }, {\"Code\": \"4452\", \"Name\": \"揭阳市\" }, {\"Code\": \"4453\", \"Name\": \"云浮市\" }] }, {\"Code\": \"45\", \"Name\": \"广西壮族自治区\", \"level\": [ {\"Code\": \"45\", \"Name\": \"全部\" }, {\"Code\": \"4501\", \"Name\": \"南宁市\" }, {\"Code\": \"4502\", \"Name\": \"柳州市\" }, {\"Code\": \"4503\", \"Name\": \"桂林市\" }, {\"Code\": \"4504\", \"Name\": \"梧州市\" }, {\"Code\": \"4505\", \"Name\": \"北海市\" }, {\"Code\": \"4506\", \"Name\": \"防城港市\" }, {\"Code\": \"4507\", \"Name\": \"钦州市\" }, {\"Code\": \"4508\", \"Name\": \"贵港市\" }, {\"Code\": \"4509\", \"Name\": \"玉林市\" }, {\"Code\": \"4510\", \"Name\": \"百色市\" }, {\"Code\": \"4511\", \"Name\": \"贺州市\" }, {\"Code\": \"4512\", \"Name\": \"河池市\" }, {\"Code\": \"4513\", \"Name\": \"来宾市\" }, {\"Code\": \"4514\", \"Name\": \"崇左市\" }] }, {\"Code\": \"46\", \"Name\": \"海南省\", \"level\": [ {\"Name\": \"全部\", \"Code\": \"46\" }, {\"Name\": \"省直辖\", \"Code\": \"4690\" }, {\"Name\": \"海口市\", \"Code\": \"4601\" }, {\"Name\": \"三亚市\", \"Code\": \"4602\" }, {\"Name\": \"三沙市\", \"Code\": \"4603\" }] }, {\"Code\": \"50\", \"Name\": \"重庆市\", \"level\": [ {\"Name\": \"所有\", \"Code\": \"50\" }, {\"Name\": \"万州区\", \"Code\": \"500101\" }, {\"Name\": \"涪陵区\", \"Code\": \"500102\" }, {\"Name\": \"渝中区\", \"Code\": \"500103\" }, {\"Name\": \"大渡口区\", \"Code\": \"500104\" }, {\"Name\": \"江北区\", \"Code\": \"500105\" }, {\"Name\": \"沙坪坝区\", \"Code\": \"500106\" }, {\"Name\": \"九龙坡区\", \"Code\": \"500107\" }, {\"Name\": \"南岸区\", \"Code\": \"500108\" }, {\"Name\": \"北碚区\", \"Code\": \"500109\" }, {\"Name\": \"綦江区\", \"Code\": \"500110\" }, {\"Name\": \"大足区\", \"Code\": \"500111\" }, {\"Name\": \"渝北区\", \"Code\": \"500112\" }, {\"Name\": \"巴南区\", \"Code\": \"500113\" }, {\"Name\": \"黔江区\", \"Code\": \"500114\" }, {\"Name\": \"长寿区\", \"Code\": \"500115\" }, {\"Name\": \"江津区\", \"Code\": \"500116\" }, {\"Name\": \"合川区\", \"Code\": \"500117\" }, {\"Name\": \"永川区\", \"Code\": \"500118\" }, {\"Name\": \"南川区\", \"Code\": \"500119\" }, {\"Name\": \"璧山区\", \"Code\": \"500120\" }, {\"Name\": \"铜梁区\", \"Code\": \"500151\" }, {\"Name\": \"潼南区\", \"Code\": \"500152\" }, {\"Name\": \"荣昌区\", \"Code\": \"500153\" }, {\"Name\": \"开州区\", \"Code\": \"500154\" }, {\"Name\": \"梁平区\", \"Code\": \"500155\" }, {\"Name\": \"武隆区\", \"Code\": \"500156\" }, {\"Name\": \"城口县\", \"Code\": \"500229\" }, {\"Name\": \"丰都县\", \"Code\": \"500230\" }, {\"Name\": \"垫江县\", \"Code\": \"500231\" }, {\"Name\": \"忠县\", \"Code\": \"500233\" }, {\"Name\": \"云阳县\", \"Code\": \"500235\" }, {\"Name\": \"奉节县\", \"Code\": \"500236\" }, {\"Name\": \"巫山县\", \"Code\": \"500237\" }, {\"Name\": \"巫溪县\", \"Code\": \"500238\" }, {\"Name\": \"石柱土家族自治县\", \"Code\": \"500240\" }, {\"Name\": \"秀山土家族苗族自治县\", \"Code\": \"500241\" }, {\"Name\": \"酉阳土家族苗族自治县\", \"Code\": \"500242\" }, {\"Name\": \"彭水苗族土家族自治县\", \"Code\": \"500243\" }] }, {\"Code\": \"51\", \"Name\": \"四川省\", \"level\": [ {\"Code\": \"51\", \"Name\": \"全部\" }, {\"Code\": \"5101\", \"Name\": \"成都市\" }, {\"Code\": \"5103\", \"Name\": \"自贡市\" }, {\"Code\": \"5104\", \"Name\": \"攀枝花市\" }, {\"Code\": \"5105\", \"Name\": \"泸州市\" }, {\"Code\": \"5106\", \"Name\": \"德阳市\" }, {\"Code\": \"5107\", \"Name\": \"绵阳市\" }, {\"Code\": \"5108\", \"Name\": \"广元市\" }, {\"Code\": \"5109\", \"Name\": \"遂宁市\" }, {\"Code\": \"5110\", \"Name\": \"内江市\" }, {\"Code\": \"5111\", \"Name\": \"乐山市\" }, {\"Code\": \"5113\", \"Name\": \"南充市\" }, {\"Code\": \"5114\", \"Name\": \"眉山市\" }, {\"Code\": \"5115\", \"Name\": \"宜宾市\" }, {\"Code\": \"5116\", \"Name\": \"广安市\" }, {\"Code\": \"5117\", \"Name\": \"达州市\" }, {\"Code\": \"5118\", \"Name\": \"雅安市\" }, {\"Code\": \"5119\", \"Name\": \"巴中市\" }, {\"Code\": \"5120\", \"Name\": \"资阳市\" }, {\"Code\": \"5132\", \"Name\": \"阿坝藏族羌族自治州\" }, {\"Code\": \"5133\", \"Name\": \"甘孜藏族自治州\" }, {\"Code\": \"5134\", \"Name\": \"凉山彝族自治州\" }] }, {\"Code\": \"52\", \"Name\": \"贵州省\", \"level\": [ {\"Code\": \"52\", \"Name\": \"全部\" }, {\"Code\": \"5201\", \"Name\": \"贵阳市\" }, {\"Code\": \"5202\", \"Name\": \"六盘水市\" }, {\"Code\": \"5203\", \"Name\": \"遵义市\" }, {\"Code\": \"5204\", \"Name\": \"安顺市\" }, {\"Code\": \"5224\", \"Name\": \"毕节地区\" }, {\"Code\": \"5226\", \"Name\": \"黔东南苗族侗族自治州\" }, {\"Code\": \"5227\", \"Name\": \"黔南布依族苗族自治州\" }] }, {\"Code\": \"53\", \"Name\": \"云南省\", \"level\": [ {\"Code\": \"53\", \"Name\": \"全部\" }, {\"Code\": \"5301\", \"Name\": \"昆明市\" }, {\"Code\": \"5303\", \"Name\": \"曲靖市\" }, {\"Code\": \"5304\", \"Name\": \"玉溪市\" }, {\"Code\": \"5305\", \"Name\": \"保山市\" }, {\"Code\": \"5306\", \"Name\": \"昭通市\" }, {\"Code\": \"5307\", \"Name\": \"丽江市\" }, {\"Code\": \"5308\", \"Name\": \"思茅市\" }, {\"Code\": \"5309\", \"Name\": \"临沧市\" }, {\"Code\": \"5323\", \"Name\": \"楚雄彝族自治州\" }, {\"Code\": \"5326\", \"Name\": \"文山壮族苗族自治州\" }, {\"Code\": \"5329\", \"Name\": \"大理白族自治州\" }, {\"Code\": \"5333\", \"Name\": \"怒江傈僳族自治州\" }, {\"Code\": \"5334\", \"Name\": \"迪庆藏族自治州\" }] }, {\"Code\": \"54\", \"Name\": \"西藏自治区\", \"level\": [ {\"Code\": \"54\", \"Name\": \"全部\" }, {\"Code\": \"5401\", \"Name\": \"拉萨市\" }, {\"Code\": \"5421\", \"Name\": \"昌都地区\" }, {\"Code\": \"5424\", \"Name\": \"那曲地区\" }, {\"Code\": \"5425\", \"Name\": \"阿里地区\" }, {\"Code\": \"5426\", \"Name\": \"林芝地区\" }] }, {\"Code\": \"61\", \"Name\": \"陕西省\", \"level\": [ {\"Code\": \"61\", \"Name\": \"全部\" }, {\"Code\": \"6101\", \"Name\": \"西安市\" }, {\"Code\": \"6102\", \"Name\": \"铜川市\" }, {\"Code\": \"6103\", \"Name\": \"宝鸡市\" }, {\"Code\": \"6104\", \"Name\": \"咸阳市\" }, {\"Code\": \"6105\", \"Name\": \"渭南市\" }, {\"Code\": \"6106\", \"Name\": \"延安市\" }, {\"Code\": \"6107\", \"Name\": \"汉中市\" }, {\"Code\": \"6108\", \"Name\": \"榆林市\" }, {\"Code\": \"6109\", \"Name\": \"安康市\" }, {\"Code\": \"6110\", \"Name\": \"商洛市\" }] }, {\"Code\": \"62\", \"Name\": \"甘肃省\", \"level\": [ {\"Code\": \"62\", \"Name\": \"全部\" }, {\"Code\": \"6201\", \"Name\": \"兰州市\" }, {\"Code\": \"6202\", \"Name\": \"嘉峪关市\" }, {\"Code\": \"6203\", \"Name\": \"金昌市\" }, {\"Code\": \"6204\", \"Name\": \"白银市\" }, {\"Code\": \"6205\", \"Name\": \"天水市\" }, {\"Code\": \"6206\", \"Name\": \"武威市\" }, {\"Code\": \"6207\", \"Name\": \"张掖市\" }, {\"Code\": \"6208\", \"Name\": \"平凉市\" }, {\"Code\": \"6209\", \"Name\": \"酒泉市\" }, {\"Code\": \"6210\", \"Name\": \"庆阳市\" }, {\"Code\": \"6211\", \"Name\": \"定西市\" }, {\"Code\": \"6212\", \"Name\": \"陇南市\" }, {\"Code\": \"6229\", \"Name\": \"临夏回族自治州\" }] }, {\"Code\": \"63\", \"Name\": \"青海省\", \"level\": [ {\"Code\": \"63\", \"Name\": \"全部\" }, {\"Code\": \"6301\", \"Name\": \"西宁市\" }, {\"Code\": \"6321\", \"Name\": \"化隆回族自治县\" }, {\"Code\": \"6322\", \"Name\": \"海北藏族自治州\" }, {\"Code\": \"6323\", \"Name\": \"黄南藏族自治州\" }, {\"Code\": \"6326\", \"Name\": \"果洛藏族自治州\" }, {\"Code\": \"6327\", \"Name\": \"玉树藏族自治州\" }] }, {\"Code\": \"64\", \"Name\": \"宁夏回族自治区\", \"level\": [ {\"Code\": \"64\", \"Name\": \"全部\" }, {\"Code\": \"6401\", \"Name\": \"银川市\" }, {\"Code\": \"6402\", \"Name\": \"石嘴山市\" }, {\"Code\": \"6403\", \"Name\": \"吴忠市\" }, {\"Code\": \"6404\", \"Name\": \"固原市\" }, {\"Code\": \"6405\", \"Name\": \"中卫市\" }] }, {\"Code\": \"65\", \"Name\": \"新疆维吾尔自治区\", \"level\": [ {\"Code\": \"65\", \"Name\": \"全部\" }, {\"Code\": \"6501\", \"Name\": \"乌鲁木齐市\" }, {\"Code\": \"6502\", \"Name\": \"克拉玛依市\" }, {\"Code\": \"6522\", \"Name\": \"哈密地区\" }, {\"Code\": \"6523\", \"Name\": \"昌吉回族自治州\" }, {\"Code\": \"6527\", \"Name\": \"博尔塔拉蒙古自治州\" }, {\"Code\": \"6528\", \"Name\": \"巴音郭楞蒙古自治州\" }, {\"Code\": \"6540\", \"Name\": \"伊犁哈萨克自治州\" }, {\"Code\": \"6543\", \"Name\": \"阿勒泰地区\" }] }, {\"Code\": \"66\", \"Name\": \"新疆生产建设兵团\", \"level\": [ {\"Code\": \"66\", \"Name\": \"全部\" }, {\"Code\": \"6601\", \"Name\": \"第一师\" }, {\"Code\": \"6602\", \"Name\": \"第二师\" }, {\"Code\": \"6603\", \"Name\": \"第三师\" }, {\"Code\": \"6604\", \"Name\": \"第四师\" }, {\"Code\": \"6605\", \"Name\": \"第五师\" }, {\"Code\": \"6606\", \"Name\": \"第六师\" }, {\"Code\": \"6607\", \"Name\": \"第七师\" }, {\"Code\": \"6608\", \"Name\": \"第八师\" }, {\"Code\": \"6609\", \"Name\": \"第九师\" }, {\"Code\": \"6610\", \"Name\": \"第十师\" }, {\"Code\": \"6611\", \"Name\": \"第十一师\" }, {\"Code\": \"6612\", \"Name\": \"第十二师\" }, {\"Code\": \"6613\", \"Name\": \"第十三师\" }, {\"Code\": \"6614\", \"Name\": \"第十四师\" }, {\"Code\": \"6615\", \"Name\": \"直属\" }] }, {\"Code\": \"71\", \"Name\": \"台湾省\", \"level\": [ {\"Code\": \"\", \"Name\": \"所有\" }] }, {\"Code\": \"81\", \"Name\": \"香港特别行政区\", \"level\": [ {\"Code\": \"\", \"Name\": \"所有\" }] }, {\"Code\": \"82\", \"Name\": \"澳门特别行政区\", \"level\": [ {\"Code\": \"\", \"Name\": \"所有\" }] }, {\"Code\": \"99\", \"Name\": \"其他地区\", \"level\": [ {\"Code\": \"\", \"Name\": \"所有\" }] }]";
        String cj = "[{\"Code\": \"45\", \"Name\": \"广西壮族自治区\", \"level\": [ {\"Code\": \"45\", \"Name\": \"全部\" }, {\"Code\": \"4501\", \"Name\": \"南宁市\" }, {\"Code\": \"4502\", \"Name\": \"柳州市\" }, {\"Code\": \"4503\", \"Name\": \"桂林市\" }, {\"Code\": \"4504\", \"Name\": \"梧州市\" }, {\"Code\": \"4505\", \"Name\": \"北海市\" }, {\"Code\": \"4506\", \"Name\": \"防城港市\" }, {\"Code\": \"4507\", \"Name\": \"钦州市\" }, {\"Code\": \"4508\", \"Name\": \"贵港市\" }, {\"Code\": \"4509\", \"Name\": \"玉林市\" }, {\"Code\": \"4510\", \"Name\": \"百色市\" }, {\"Code\": \"4511\", \"Name\": \"贺州市\" }, {\"Code\": \"4512\", \"Name\": \"河池市\" }, {\"Code\": \"4513\", \"Name\": \"来宾市\" }, {\"Code\": \"4514\", \"Name\": \"崇左市\" }] }]";
        int page = 1;
        List<CityTree> classInfos = JSONArray.parseArray(cj,CityTree.class);
        System.out.println(classInfos.size());
//        for(CityTree cityTree:classInfos){
//            String cityCode = cityTree.getCode();
//            String cityName = cityTree.getName();
//            System.out.println(cityName+"-"+cityCode);
//        }
        LawList lawer = null;
            loop3:for(CityTree cityTree:classInfos){
                String provinCode = cityTree.getCode();
                String provinName= cityTree.getName();
                System.out.println("当前："+provinName);
                List<LevelInfo> levelInfos = cityTree.getLevel();
                boolean hasNextPage = true;
                loop4:while (hasNextPage){
                    String url= "http://www.12348.gov.cn/sfbMobile/people/lawyers?cityCode="+provinCode+"&areaCode=&lawOfficeId=&goodAt=&level=&keywords=&pageNum="+page+"&pageSize=100";
                    System.out.println("请求："+url);
                    String json = getPostStr(url);
                    JSONObject obj = JSONObject.parseObject(json);
                    JSONArray jsona = JSONArray.parseArray(String.valueOf(obj.get("list")));
                    if(jsona==null||jsona.size()==0){
                        System.out.println("请求："+url+",无更多列表准备在此尝试");

                        url= "http://www.12348.gov.cn/sfbMobile/people/lawyers?cityCode="+provinCode+"&areaCode=&lawOfficeId=&goodAt=&level=&keywords=&pageNum="+page+"&pageSize=100";
                        System.out.println("请求："+url);
                         json = getPostStr(url);
                         obj = JSONObject.parseObject(json);
                         jsona = JSONArray.parseArray(String.valueOf(obj.get("list")));
                        if(jsona==null||jsona.size()==0){
                            System.out.println("请求："+url+",二次尝试无更多列表跳出");
                            hasNextPage = false;
                            page = 1;
                            break loop4;

                        }


                    }
                    loop1:for(Object objLi:jsona){
                        LawerInfo law = JSONObject.parseObject(String.valueOf(objLi),LawerInfo.class);
                        System.out.println("正在获取律师:"+law.getName());
                        lawer = new LawList();
                        lawer.setcCode(law.getXzqh());
                        lawer.setLawName(law.getName());
                        System.out.println("律师名字："+law.getName());
                        lawer.setProvince(provinName);
                        loop2:for(LevelInfo l:levelInfos){
                            if(l.getCode().equals(law.getXzqh())){
                                lawer.setCity(l.getName());
                                break loop2;
                            }
                        }
                        lawer.setLawFirm(law.getLawoffice());

                        //解析律师详情，获取律师执业编号
                        String pkid = law.getPkid();
                        String deUrl = "http://www.12348.gov.cn/sfbMobile/people/lawyer?pkid="+pkid;
                        String lawjson = getPostStr(deUrl);
                        JSONObject lawobj = JSONObject.parseObject(lawjson);
                        System.out.println(lawobj+"~~");
                        String datas = lawobj.getString("data");
                        String basics = JSONObject.parseObject(datas).getString("basic");
                        String licnumber = JSONObject.parseObject(basics).getString("licnumber");
                        lawer.setLicnumber(licnumber);
                        lawListMapper.insertLawList(lawer);
                    }
//                    System.out.println(json);
                    page++;
                }

            }
            return true;
    }


    private  String getPostStr(String url){
        String dataJson =  "";
        String result = "";
        try {
            System.setProperty("http.proxyHost", "127.0.0.1");
            System.setProperty("http.proxyPort", "19180");
            // 对https也开启代理
            System.setProperty("https.proxyHost", "127.0.0.1");
            System.setProperty("https.proxyPort", "19180");
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("GET");
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
//            for (String key : map.keySet()) {
//                System.err.println(key + "--->" + map.get(key));
//                System.out.println();
//            }
            // 定义 BufferedReader输入流来读取URL的响应
            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            /**
             * 返回结果示例
             */
//            System.err.println("result:" + result);
//            System.out.println(result);
            dataJson = result;

        } catch (Exception e) {
            System.err.printf("获取dataJson失败！"+e.getMessage());
//            e.printStackTrace(System.err);
        }
        return result;

    }

}
